package com.young.coordin.server.model;

import com.young.common.util.IPUtil;
import com.young.interfaces.coordin.model.HeartbeatDTO;
import com.young.interfaces.coordin.model.RegisterDTO;

import java.util.Date;

/**
 * 注册DO对象
 * Created by Admin on 2020/12/10.
 */
public class RegisterDO {
    /**
     * 主键id
     */
    private Long id;
    /**
     * 注册时间
     */
    private Date registerTime;
    /**
     * 上一次心跳的时间
     */
    private Date lastHeartbeatTime;
    /**
     * 心跳版本号
     */
    private Long heartbeatVersion;
    /**
     * 客户端ip
     * 和端口号唯一标识一个客户端服务
     */
    private String clientIp;
    /**
     * 客户端端口号
     */
    private Integer clientPort;
    /**
     * 服务端ip
     */
    private String serverIp;

    /**
     * 对象转换
     * @param heartbeatDTO
     * @return
     */
    public static RegisterDO valueOf(HeartbeatDTO heartbeatDTO) {
        RegisterDO registerDO = new RegisterDO();
        registerDO.setRegisterTime(new Date());
        registerDO.setLastHeartbeatTime(new Date());
        registerDO.setHeartbeatVersion(heartbeatDTO.getVersion());
        registerDO.setClientIp(heartbeatDTO.getIp());
        registerDO.setClientPort(heartbeatDTO.getPort());
        registerDO.setServerIp(IPUtil.getLocalIp());
        return registerDO;
    }

    /**
     * 对象转换
     * @param registerDTO
     * @return
     */
    public static RegisterDO valueOf(RegisterDTO registerDTO) {
        RegisterDO registerDO = new RegisterDO();
        registerDO.setRegisterTime(new Date());
        registerDO.setLastHeartbeatTime(new Date());
        registerDO.setHeartbeatVersion(1L);
        registerDO.setClientIp(registerDTO.getIp());
        registerDO.setClientPort(registerDTO.getPort());
        registerDO.setServerIp(IPUtil.getLocalIp());
        return registerDO;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("{");
        sb.append("\"id\":").append(id);
        sb.append(", \"registerTime\":").append(registerTime != null ? "\"" : "").append(registerTime).append(registerTime != null ? "\"" : "");
        sb.append(", \"lastHeartbeatTime\":").append(lastHeartbeatTime != null ? "\"" : "").append(lastHeartbeatTime).append(lastHeartbeatTime != null ? "\"" : "");
        sb.append(", \"heartbeatVersion\":").append(heartbeatVersion);
        sb.append(", \"clientIp\":").append(clientIp != null ? "\"" : "").append(clientIp).append(clientIp != null ? "\"" : "");
        sb.append(", \"clientPort\":").append(clientPort);
        sb.append(", \"serverIp\":").append(serverIp != null ? "\"" : "").append(serverIp).append(serverIp != null ? "\"" : "");
        sb.append('}');
        return sb.toString();
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public Date getLastHeartbeatTime() {
        return lastHeartbeatTime;
    }

    public void setLastHeartbeatTime(Date lastHeartbeatTime) {
        this.lastHeartbeatTime = lastHeartbeatTime;
    }

    public Long getHeartbeatVersion() {
        return heartbeatVersion;
    }

    public void setHeartbeatVersion(Long heartbeatVersion) {
        this.heartbeatVersion = heartbeatVersion;
    }

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }

    public Integer getClientPort() {
        return clientPort;
    }

    public void setClientPort(Integer clientPort) {
        this.clientPort = clientPort;
    }

    public String getServerIp() {
        return serverIp;
    }

    public void setServerIp(String serverIp) {
        this.serverIp = serverIp;
    }
}
